package com.dtw.ditouwang.views;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SoundEffectConstants;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import com.dtw.ditouwang.R;
import com.dtw.ditouwang.utils.DensityUtil;

/**
 * Created by Administrator on 2018/1/30 0030.
 */

public class ProgressView extends View {
    int defaultWidth = DensityUtil.dip2px(50), defaultHeight = DensityUtil.dip2px(50);
    int startColor, endColor, startAngle, endAngle, textColor;
    float animProgress, progress, stockWidth;
    boolean showText;
    String text = "";
    Paint arcPaint, textPaint;
    ValueAnimator valueAnimator;
    Paint backCirlPaint;

    public ProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ProgressView);
        startColor = typedArray.getColor(R.styleable.ProgressView_StartColor, Color.TRANSPARENT);
        endColor = typedArray.getColor(R.styleable.ProgressView_EndColor, Color.BLACK);
        startAngle = typedArray.getInt(R.styleable.ProgressView_StartAngle, 270);
        endAngle = typedArray.getInt(R.styleable.ProgressView_EndAngle, 270+360);
        animProgress = progress = typedArray.getFloat(R.styleable.ProgressView_Progress, 0);
        stockWidth = typedArray.getDimension(R.styleable.ProgressView_StockWidth, DensityUtil.dip2px(2));
        showText = typedArray.getBoolean(R.styleable.ProgressView_ShowText, true);
        textColor = typedArray.getColor(R.styleable.ProgressView_TextColor, Color.WHITE);
        typedArray.recycle();
    }

    public void setText(String text) {
        this.text = text;
    }

    public void setTextColor(int color){
        this.textColor=color;
    }

    public void setProgress(final float progress) {
        this.progress = progress;
        Log.i("dtw", "progress:" + progress);
        if (valueAnimator != null && valueAnimator.isRunning()) {
            valueAnimator.cancel();
        }
        valueAnimator = ValueAnimator.ofFloat(-0.3f, progress);
        valueAnimator.setInterpolator(new DecelerateInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                ProgressView.this.animProgress = (float) animation.getAnimatedValue();
                postInvalidateOnAnimation();
            }
        });
        valueAnimator.start();
    }

    public void initPaint() {
        SweepGradient sweepGradient = new SweepGradient(getMeasuredWidth() / 2, getMeasuredHeight() / 2, new int[]{startColor, endColor}, new float[]{startAngle, endAngle / 360f});
        arcPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        arcPaint.setStyle(Paint.Style.STROKE);
        arcPaint.setStrokeWidth(stockWidth);
        arcPaint.setStrokeCap(Paint.Cap.ROUND);
        arcPaint.setShader(sweepGradient);

        textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setColor(textColor);
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setTextSize(getMeasuredWidth() / 3);

        backCirlPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        backCirlPaint.setColor(Color.WHITE);
        backCirlPaint.setAlpha(30);
        backCirlPaint.setStyle(Paint.Style.STROKE);
        backCirlPaint.setStrokeWidth(stockWidth);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
//        switch (event.getAction()) {
//            case MotionEvent.ACTION_DOWN:
//                playSoundEffect(SoundEffectConstants.CLICK);
//                setProgress(progress);
//                break;
//        }
        return super.onTouchEvent(event);
    }

    private int getSize(int defaultValue, int measureSpec) {
        int mode = MeasureSpec.getMode(measureSpec);
        int length = MeasureSpec.getSize(measureSpec);
        switch (mode) {
            case MeasureSpec.UNSPECIFIED:
                length = defaultValue;
                break;
            case MeasureSpec.AT_MOST:
                if (length > defaultValue) {
                    length = defaultValue;
                }
                break;
            case MeasureSpec.EXACTLY:
                break;
        }
        return length;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(getSize(defaultWidth, widthMeasureSpec), getSize(defaultHeight, heightMeasureSpec));
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        initPaint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawArc(new RectF(stockWidth, stockWidth, getMeasuredWidth() - stockWidth, getMeasuredHeight() - stockWidth),
        0,400,false,backCirlPaint);
        canvas.drawArc(new RectF(stockWidth, stockWidth, getMeasuredWidth() - stockWidth, getMeasuredHeight() - stockWidth),
                startAngle + 5, endAngle * animProgress , false, arcPaint);
        if (showText) {
            canvas.drawText(text, getMeasuredWidth() / 2, getMeasuredHeight() / 2 + (-textPaint.ascent() - textPaint.descent()) / 2, textPaint);
        }
    }
}
